<?php
/*
+--------------------------------------------------------------------------
|   Web: http://www.enbac.com 
|   Started date : 30/06/2008
+---------------------------------------------------------------------------
|   > Script written by Nova
+---------------------------------------------------------------------------
*/

if (eregi(basename( __FILE__ ), $_SERVER['PHP_SELF'])) {
	print "<h1>Incorrect access</h1>You cannot access this file directly.";
	exit;
}

class ajax_admin {
	function playme(){
		$code = AZLib::getParam('code');
		switch( $code ){
			case 'ban_nick':
				$this->fn_ban_nick();
				break;
			case 'un_ban_nick_profile':
				$this->fn_unban_nick_profile();
				break;		
			case 'add_admin':
				$this->fn_add_admin();
				break;
			case 'up_item':
				$this->up_item();
				break;
			case 'down_item':
				$this->down_item();
				break;
			case 'no_source_item':
				$this->fn_no_source_item();
				break;
			case 'exit_no_source_item':
				$this->fn_exit_no_source_item();
				break;	
			case 'in_valid':
				$this->invalid_item();
				break;
			case 'change_status_item':
				$this->change_status_item();
				break;				
			case 'item_del_admin':
				$this->fn_item_del_admin();
				break;					
			case 'reset_pas':
				$this->fn_reset_pas();
				break;		
			case 'grant_permit':
				$this->grant_permit();
				break;		
			case 'grant_category':
				$this->grant_category();
				break;
			case 'active_user':
				$this->fn_active_user();
				break;
			case 'send_email_active':
				$this->fn_send_email_active();
				break;
			case 'add_up_item':
				$this->fn_add_up_item(1);
				break;
			case 'minus_up_item':
				$this->fn_add_up_item(0);
				break;						
			case 'edit_phone_verify':
				$this->edit_phone_verify();
				break;						
			case 'note_phone_verify':
				$this->note_phone_verify();
				break;						
			case 'note_sms_uptin':
				$this->note_sms_uptin();
				break;
			case 'note_sms_phone_verify':
				$this->note_sms_phone_verify();
				break;
			case 'note_user_active_sms':
				$this->note_user_active_sms();
				break;
                        case 'note_payment_ebp':
				$this->note_payment_ebp();
				break;
			case 'processed_bad_content':
				$this->fn_processed_bad_content();
				break;
			case 'tracking_user':
				$this->fn_tracking_user();
				break;	
			case 'tag_search':
				$this->fn_tag_search();
				break;
			case 'del_tag_search':
				$this->fn_del_tag_search();
				break;
			case 'lock_topic':
				$this->fn_lock_topic();
				break;	
			case 'del_item_reason':
				$this->fn_del_item_reason();
				break;
			case 'del_reason':
				$this->fn_del_reason();
				break;				
			case 'valid_all_item':
				$this->fn_valid_all_item();
				break;	
			case 'add_product_hot':
				$this->fn_add_product_hot();
				break;		
			case 'del_product_hot':
				$this->fn_del_product_hot();
				break;	
			case 'del_item_product_hot':
				$this->fn_del_item_product_hot();
				break;	
			case 'del_static_cache':
				$this->fn_del_static_cache();
				break;
			case 'user_same':
				$this->user_same();
				break;
			case 'del_user_same':
				$this->del_user_same();
				break;
			case 'admin_notice_user':
				$this->admin_notice_user();
				break;	
			case 'add_edit_ban_ip':
				$this->add_edit_ban_ip();
				break;	
			case 'xyly_tinspam':
				$this->xyly_tinspam();
				break;		
			case 'update_sale_name':
				$this->update_sale_name();
				break;
			case 'update_exist_acc':
				$this->update_exist_acc();
				break;
			case 'update_cpc_left':
				$this->update_cpc_left();
				break;
			case 'minus_take_care':
				$this->minus_take_care();
				break;
			default:
				$this->home();
				break;
		}
	}
	
	function home(){
		global $display;
		die("Nothing to do...");
	}

	function fn_ban_nick(){//Khoá thành viên
		if(!User::is_login()){
			echo 'no_login';	
			exit;
		}
		if(!User::have_permit(array(ADMIN_USER,MOD_LOCK_USER))){
			echo 'no_perm';	
			exit;
		}
		$ban_date 		= intval(AZLib::getParam('ban_date'));	
		$type_lock 		= intval(AZLib::getParam('type_lock'));	
		$reason_lock 	= AZLib::getParam('reason_lock');	
		$user_id 		= intval(AZLib::getParam('user_id'));
		$user_name 		= AZLib::getParam('user_name');
			
		//neu user_id da tung bi khoa thi xoa ban ghi nay di.
		/*if($user_id){
			$sql = 'DELETE FROM user_lock WHERE user_id="'.$user_id.'"  AND type IN (0,1)';
			DB::query($sql);
		}*/
		// update lich up tin
		DB::query("UPDATE up_item_schedule SET status=0 WHERE user_id = $user_id AND status = 1");
				
		if($type_lock==2 && $user_id!=4){// khoá vĩnh viễn 
			require_once 'modules/UserAdmin/forms/UserAdmin.php';
			ListUserAdminForm::lock_user($user_id);			
			DB::update('user',array('block_time'=>"-1"),'id ='.$user_id);
			DB::insert('user_lock',array('time'=>TIME_NOW,'user_id'=>$user_id,'user_name'=>$user_name,'type'=>1,'note'=>$reason_lock,'admin_id'=>User::id(),'admin_name'=>User::user_name()));
			User::getUser($user_id,0,1);
			echo "lock_not_cookies";
			exit;
		}
		elseif($type_lock==3 && $user_id!=4){// khoá vĩnh viễn + khoá cookies
			require_once 'modules/UserAdmin/forms/UserAdmin.php';
			ListUserAdminForm::lock_user($user_id);				
			DB::update('user',array('block_time'=>"-1"),'id ='.$user_id);
			DB::insert('user_lock',array('time'=>TIME_NOW,'user_id'=>$user_id,'user_name'=>$user_name,'type'=>3,'note'=>$reason_lock,'admin_id'=>User::id(),'admin_name'=>User::user_name()));
			User::getUser($user_id,0,1);
			echo "lock_cookies";
			exit;
		}				
		elseif($ban_date>0 && $user_id!=4){//Khoá có thời hạn			
			$time_start = mktime(24,0,0,date('m'),date('d'),date('Y'));
			$time_expire = $time_start+$ban_date*24*3600;						
			if(DB::query("UPDATE user SET block_time=$time_expire WHERE id = $user_id")){	
				DB::insert('user_lock',array('time'=>TIME_NOW,'time_expire'=>$time_expire,'user_id'=>$user_id,'user_name'=>$user_name,'type'=>0,'note'=>$reason_lock,'admin_id'=>User::id(),'admin_name'=>User::user_name()));
				User::getUser($user_id,0,1);
				
				// Kiem duyet tin khi khoa thanh vien
				$arr_data = array('user_id'=>$user_id,						 
						  'reason_invalid'=>'Khóa thành viên nên khóa tất cả Topic, '.$reason_lock
						  );
				$this->lockItemWhenLockAccount($arr_data, 1);
				// end kiem duyet tin khi khoa thanh vien
				
				echo '<font color="red"><strong>'.date('H:i d/m/y ',$time_expire).'</strong></font>';
				exit;	
			}
			else{
				echo "unsuccess";
				exit;	
			}
		}
		else {
			echo "invalid";
			exit;	
		}
	}
	
	function fn_add_admin(){
		if(!User::is_admin()){
			echo 'no_perm';	
			exit;
		}
		
		$id_admin 			= (int)Url::get('id_admin');
		$user_name_admin 	= AZLib::getParam('user_name_admin');
		$group_id 		 	= (int)AZLib::getParam('group_id');
		
		if($group_id==9 && !User::is_root()){//Nếu phân quyền root
			echo 'no_perm';	
			exit;
		}
		
		if($id_admin>0 || $user_name_admin!=''){
			if($id_admin)
			$user = User::getUser($id_admin);
			else{
				$user=DB::select('user','user_name="'.$user_name_admin.'"');
			}
			
			if($user){
				$groups=User::get_groups($user['gids']);
				
				if(isset($groups[$group_id])){
					User::getUser($user['id'],0,1);
					echo "exist_admin";
					exit;
				}
				else{
					if($user['gids']=='0'){
						$user['gids']=$group_id;
					}
					else{
						$user['gids'].='|'.$group_id;
					}
					
					if(DB::query('UPDATE user SET gids="'.$user['gids'].'" WHERE id='.$user['id'])){
						User::getUser($user['id'],0,1);
						echo "success";		
						exit;
					}
					else {
						echo "unsuccess";		
						exit;
					}
				}
			}
			else{
				echo "no_exist";
				exit;
			}
		}
		else {
			echo "invalid";
			exit;	
		}
	}
	
	function grant_permit(){
		if(!User::is_admin()){
			echo 'no_perm';	
			exit;
		}
		
		$type				= (int)Url::get('type');
		$ref_id				= (int)Url::get('ref_id');
		$pids 				= AZLib::getParam('pids');
		
		if($ref_id==0 && in_array($ref_id,array(1,9))){//Không được phân quyền cho admin và root
			echo "unsuccess";	
			exit;
		}
		
		if($ref_id>0){
			if($type==0 && !isset(CGlobal::$group[$ref_id])){
				echo "invalid";
				exit;	
			}
			if($type==1 && !User::getUser($ref_id)){
				echo "invalid";
				exit;	
			}
			
			$permit=DB::select('user_permit','type='.$type.' AND ref_id='.$ref_id);
			if(!$permit){
				$permit=array(
								'type'	=>$type,
								'ref_id'=>$ref_id,
								'pids'	=>$pids,
								'cids'	=>$permit['cids']
								);
			}
			else{
				$permit['pids']=$pids;
			}
			
			if(DB::insert('user_permit',$permit,true)){
				User::getUser($ref_id,0,1);
				User::get_permits(1,$ref_id);
				echo "success";		
				exit;
			}
			else {
				echo "unsuccess";		
				exit;
			}
		}
		else {
			echo "invalid";
			exit;	
		}
	}
	
	function grant_category(){
		if(!User::is_admin()){
			echo 'no_perm';	
			exit;
		}
		
		$user_id			= (int)Url::get('user_id');
		$cids 				= AZLib::getParam('cids');
		
		if($user_id>0){
			$user=User::getUser($user_id);
			if(!$user){
				echo "invalid";
				exit;	
			}
			
			$cid_arr=explode(',',$cids);
			$cids='';
			if($cid_arr){
				AZLib::getCats();
				
				foreach ($cid_arr as $cid){
					if(isset(CGlobal::$allCategories[$cid]))
					$cids.=($cids?',':'').$cid;
				}
			}
			
			$permit=DB::select('user_permit','type=1 AND ref_id='.$user_id);
			if(!$permit){
				$permit=array(
								'type'	=>1,
								'ref_id'=>$user_id,
								'pids'	=>$permit['pids'],
								'cids'	=>$cids
								);
			}
			else{
				$permit['cids']=$cids;
			}
			
			if(DB::insert('user_permit',$permit,true)){
				User::getUser($user_id,0,1);
				echo "success";		
				exit;
			}
			else {
				echo "unsuccess";		
				exit;
			}
		}
		else {
			echo "invalid";
			exit;	
		}
	}
	
	function up_item(){
		if(!User::is_login()){
			echo "not_login";
			exit;
		}
		//elseif(!User::is_admin() && (User::is_block()  || User::level()==0)){//Nếu ko fải admin mà bị khóa hoặc chưa là THÀNH VIÊN CHÍNH THỨC
		elseif(!User::is_admin() && User::is_block()){//Nếu ko fải admin mà bị khóa hoặc chưa là THÀNH VIÊN CHÍNH THỨC
			echo "no_perm";
			exit;
		}
		
		$item_id  	 	= (int)Url::get('item_id');
		$up_tk  	 	= (int)Url::get('up');//0:up free; 1: up tài khoản
		
		if($item_id)
		{
			$item 			= Item::get_item($item_id);
			$item_memcache 	= $item;
			$item_update	= array();
			
			$have_permit	= false;
			$up_log_sql 	= '';
			
			if(!$item){
				echo "no_perm";
				exit;
			}
			
			if($item["state"]==1  || $item["status"]!=1){//Nêu bị khóa hoặc tin bị xóa hoặc kiểm duyệt...
				echo "lock_topic";
				exit;
			}
			
			$user 		= DB::fetch('SELECT id, user_name, up_item, block_time, invalid_time FROM user WHERE id='.$item['user_id']);
			
			if($user['block_time'] > TIME_NOW || $user['block_time']==-1){
				echo "no_perm";
				exit;
			}
			
			$up_free 		= 0;
			$is_up_free 	= false;
			
			$up_type 		= 0;//Kiểu up: 0: up free; 1: up tay; 2: up lịch; 3: up lẻ
			$up_zone_id 	= 0;//Zone ID
			$up_top_cat		= $item['level_1_catid'];//TopCatID
			$up_cat_id		= $item['category_id'];//CatID
			
			$mid_noon 	= mktime(12,0,0,date('m',TIME_NOW),date('d',TIME_NOW),date('Y',TIME_NOW));//thời điểm giữa trưa cùng ngày
			
			//Là up free && hết lượt up tài khoản && ko fải là admin
			if(!$up_tk && User::level() && !User::have_permit(ADMIN_ITEM) && !User::have_cat_permit($item['category_id']))
			{
				if(TIME_NOW < $mid_noon)//Buổi sáng
				{
					$up_free 	= DB::select("up_free","user_id=". $user['id']. " AND catid = $up_top_cat AND date ='".date("Ymd",TIME_NOW)."S'");
					$str_time 	= 'buổi sáng';
					$type_time 	= 1;//buổi sáng
				}
				else//Buổi chiều tối
				{
					$up_free = DB::select("up_free","user_id=". $user['id']. " AND catid = $up_top_cat AND date ='".date("Ymd",TIME_NOW)."C'");
					$str_time 	= 'buổi chiều tối';
					$type_time 	= 2;//buổi chiều tối
				}
				
				if(date("dmY",$item["up_time"]) == date("dmY"))//Nếu tin đã được up trong ngày
				{
					echo "time_out";
					exit;
				}
				elseif//Nếu up quá số lượt up cho phép / 1 ngày
				(
					$up_free 
					
					&& 
					
					(
						(isset(CGlobal::$UP_FREE_CAT[$up_top_cat]) && (int)(CGlobal::$UP_FREE_CAT[$up_top_cat]/2) <= (int)$up_free['up'])  
						
						|| 
						
						(!isset(CGlobal::$UP_FREE_CAT[$up_top_cat]) && (int)(UP_FREE_MAX/2) <= (int)$up_free['up'])
					)
				)
				{
					echo "time_out$type_time";
					exit;
				}
				//else =>> còn lượt up trong ngày
			}										
		
			$out_put = "";
			
			if(User::have_permit(ADMIN_ITEM) || User::have_cat_permit($item['category_id']) || $item['user_id'] == User::id())
			{	
				$main_cat 	= CGlobal::$allCategories[$up_top_cat];
				
				if($main_cat && isset($main_cat['zones'])){
					foreach ($main_cat['zones'] as $zid){
						if($zid && isset(CGlobal::$allZones[$zid])){
							
							$up_zone_id = $zid;
							
							if($main_cat['id'] != 100){
								break;
							}
						}
					}
				}
				
				$out_put 		= "Còn lượt up tin trong ngày";			
				$user_up_item 	= $user['up_item'];
				
				$up_order 		= "0";
				
				if(User::have_permit(ADMIN_ITEM) || User::have_cat_permit($item['category_id']))//BQT up
				{
					$item_update['up_time'] 	= TIME_NOW;
					$item_memcache['up_time'] 	= TIME_NOW;
					
					$out_put = "up-0";
					
					$up_log_sql="INSERT INTO up_item_log 
									(up_id, item_id, 		zone_id, 	top_cat_id, 	cat_id, 		item_title, 		user_id, 		user_name, 				up_time, 		type, 		run_order, 							up_remain, 		up_total, 			status) VALUES ";
					$up_log_sql.="	(0,		{$item['id']},	$up_zone_id,$up_top_cat,	$up_cat_id,	'{$item['name']}',	{$user['id']},	'{$user['user_name']}',	".TIME_NOW.",	$up_type,	'admin: (".User::user_name().")',	$user_up_item,	{$user['up_item']},	1)";
					DB::query($up_log_sql);
					
				}
				else//Người dùng up
				{	
					$up_time 	= TIME_NOW;
					$is_up 		= true;
					$out_put 	= "up-1";
					
					if(User::level()==0)//Chưa là thành viên chính thức
					{
						if($user['up_item']>0)//Còn lượt up tin
						{
							$up_type = 1;//up tài khoản
							
							//$re = DB::query("SELECT id,up_time FROM item WHERE user_id = ".$item['user_id']." AND id ORDER BY up_time DESC LIMIT 1");
							$re = DB::query("SELECT id,up_time,category_id,level_1_catid FROM item WHERE user_id = ".$item['user_id']." AND level_1_catid = {$item['level_1_catid']} ORDER BY up_time DESC LIMIT 1");
							
							if($re)
							{
								$old_item = mysql_fetch_assoc($re);
								
								if($old_item){
									if($old_item['up_time']>TIME_NOW-300 && $old_item['id']!=$item['id']){
										$up_time = $old_item['up_time']-300;
									}
								}
							}
							//End down time
							
							$item_update['up_time'] = $up_time;
							$item_memcache['up_time'] = $up_time;
							//DB::query('UPDATE user SET up_item = '.(int)($user['up_item']-1).' WHERE id='.$item['user_id']);
							DB::query('UPDATE user SET up_item = up_item-1 WHERE id='.$item['user_id']);
							User::getUser($item['user_id'],0,1);
							$user_up_item--;
							$up_order= "$user_up_item/".$user['up_item'];
						}
						else
						{
							echo "no_perm_offical";//echo "no_perm";
							exit;
						}
					}
					else
					{
						//Down time item nếu mới up hoặc đăng một tin nào đó chưa quá 5 phút
						//$re = DB::query("SELECT id,up_time FROM item WHERE user_id = ".$item['user_id']." AND id ORDER BY up_time DESC LIMIT 1");
						$re = DB::query("SELECT id,up_time,category_id,level_1_catid FROM item WHERE user_id = ".$item['user_id']." AND level_1_catid = {$item['level_1_catid']} ORDER BY up_time DESC LIMIT 1");
						if($re){
							$old_item = mysql_fetch_assoc($re);
							
							if($old_item)
							{
								//if($old_item['up_time']>TIME_NOW-300 && $old_item['id']!=$item['id'] && ($old_item['category_id']==$item['category_id'] || $old_item['level_1_catid']==$item['level_1_catid']))
								if($old_item['up_time']>TIME_NOW-300 && $old_item['id']!=$item['id'])
								{
									$up_time = $old_item['up_time'] - 300;
								}
							}
						}
						//End down time
						
						if($up_tk)//Up tài khoản
						{	
							if($user['up_item']>0)
							{
								$up_type = 1;//up tài khoản
								
								$item_update['up_time'] 	= $up_time;
								$item_memcache['up_time'] 	= $up_time;
								//DB::query('UPDATE user SET up_item = '.(int)($user['up_item']-1).' WHERE id='.$item['user_id']);
								DB::query('UPDATE user SET up_item = up_item-1 WHERE id='.$item['user_id']);
								User::getUser($item['user_id'],0,1);
								$user_up_item--;
								$up_order= "$user_up_item/".$user['up_item'];
							}
							/*elseif(User::isPremium())//TK trả sau
							{
								if(abs($user['up_item']) < $user['up_quota'])//TK trả sau và lượt up chưa vượt hạn mức
								{
									$up_type = 5;//up thấu chi
									
									$item_update['up_time'] 	= $up_time;
									$item_memcache['up_time'] 	= $up_time;
									
									DB::query('UPDATE user SET up_item = up_item-1 WHERE id='.$item['user_id']);
									User::getUser($item['user_id'],0,1);
									
									$user_up_item--;
									$up_order= "$user_up_item/".$user['up_item'];
								}
								else
								{
									$out_put = "up_max";
									$is_up = false;
								}
							}*/
							else 
							{
								$out_put 	= "up_over";
								$is_up 		= false;
							}
						}
						else//Up free
						{
							$out_put = "Hết lượt up tin $str_time";
							
							$is_up_free = true;
							
							$item_update['up_time'] 	= $up_time;
							$item_memcache['up_time'] 	= $up_time;
							
							$up_order 					= "Up free";
							
							if($up_free){
								DB::query("UPDATE up_free SET time_up = ".TIME_NOW.",up= up+ 1 WHERE id={$up_free['id']}");
							}
							else{
								DB::insert("up_free",array(
															'user_id'	=> $user['id'],
															'user_name'	=> $user['user_name'],
															'time_f'	=> TIME_NOW,
															'time_up'	=> TIME_NOW,
															'catid'		=> $up_top_cat,
															'date'		=> date("Ymd",TIME_NOW).(TIME_NOW < $mid_noon ? "S":"C"),
															'up'		=> 1,
															));
							}
						}
					}
					
					if($is_up)
					{
						$up_log_sql="INSERT INTO up_item_log 
											(up_id, 	item_id, 		zone_id, 		top_cat_id, 	cat_id, 	item_title, 		user_id, 		user_name, 				up_time, 		type, 		run_order, 				up_remain, 	up_total, 		status) VALUES ";
						$up_log_sql.=	"	(0,			{$item['id']},	$up_zone_id,	$up_top_cat,	$up_cat_id,	'{$item['name']}',	{$user['id']},	'{$user['user_name']}',	".TIME_NOW.",	$up_type,	'$up_order (".User::user_name().")',	$user_up_item,	{$user['up_item']},	1)";
						DB::query($up_log_sql);
						
					}
				}
			}
			else{
				$out_put = "no_perm";
			}	
			
			//Up item
			if($item_update){
				DB::update('item',$item_update,"id=$item_id");
				
				if(MEMCACHE_ON){
					AZMemcache::do_put("item:$item_id",$item_memcache);
				}
			}
			
			//return...
			echo $out_put;
			exit;
		}	
	}
	
	function down_item(){
		if(!User::is_login()){
			echo "not_login";
			exit;
		}
		$item_id= (int)Url::get('item_id');
		if($item_id){
			$item 			= Item::get_item($item_id);
			$item_memcache 	= $item;
		}
		else 
			$item=array();
		
		if($item && (User::have_permit(ADMIN_ITEM) || User::have_cat_permit($item['category_id']))){
			$up_time 		= $item['up_time'];
			$description 	= $item['description'];
			
			if(!strpos($description,REASON_DOWN_ITEM)){
				$description = $item['description'].REASON_DOWN_ITEM;		
			}								
			
			$id = DB::update('item',array('up_time'=>($up_time-TIME_DOWN),'description'=>$description),'id='.$item_id);
			
			if($id){
				if(MEMCACHE_ON){
					$item_memcache['up_time'] 	  = ($up_time-TIME_DOWN);
					$item_memcache['description'] = $description;
					AZMemcache::do_put("item:$item_id",$item_memcache);
				}
				
				echo "success";
				exit;
			}
			else {
				echo "unsuccess";
				exit;
			}
		}
		else{
			echo "no_perm";
			exit;
		}	
	}
	
	function fn_no_source_item(){		
		if(!User::is_login()){
			echo "not_login";
			exit;
		}
		
		$item_id= (int)Url::get('item_id');
		if($item_id){
			$item 			= Item::get_item($item_id);
			$item_memcache 	= $item;
		}
		else 
			$item=array();
		
				
		if($item && (User::have_permit(ADMIN_ITEM) || User::have_cat_permit($item['category_id']))){
			$brief = $item['brief'];
			if($brief!=''){
				echo "done";
				exit;
			}
			else{
				$id = DB::update('item',array('brief'=>REASON_NO_SOURCE_ITEM,'valid_time'=>TIME_NOW,'valid_user'=>User::user_name()),'id='.$item_id);
				if($id){
					if(MEMCACHE_ON){
						$item_memcache['brief']=REASON_NO_SOURCE_ITEM;
						$item_memcache['valid_time']=TIME_NOW;
						$item_memcache['valid_user']=User::user_name();
						AZMemcache::do_put("item:$item_id",$item_memcache);
					}
					echo "success";
					exit;
				}
				else{
					echo "unsuccess";
					exit;
				}
			}
		}
		else {
			echo "no_perm";
			exit;
		}
	}

	function fn_item_del_admin(){		
		if(!User::is_login()){
			echo "not_login";
			exit;
		}		

		$item_id= (int)Url::get('item_id');
		if($item_id){
			$item 	= Item::get_item($item_id);
		}
		else 
			$item=array();
		
		if($item && (User::have_permit(ADMIN_ITEM) || User::have_cat_permit($item['category_id']))){
			Item::delete_item($item_id);			
			echo "success";
			exit;			
				
		}else {
			echo "no_perm";
			exit;
		}				
	}
	
	function change_status_item(){
		$item_id	= (int)Url::get('id');
		$status		= (int)Url::get('status');
		if($item_id){
			$item 			= Item::get_item($item_id);
			$item_memcache 	= $item;
		}
		else 
			$item=array();
		
		if(!$item || (!User::have_permit(ADMIN_ITEM) && !User::have_cat_permit($item['category_id']))){
			echo "no_perm";
			exit;
		}
		
		Item::update_status($item['id'],$status);
		if ($status == 1) {
			DB::delete('bad_content','id_item = '.$item_id.' AND type = 2');
		}
		$from =  AZLib::getParam('from');
		
		if($from=='admin'){
			echo AZLib::getParam('user_id').'_'.$item_id;
		}
		else{
			echo "success";
		}
		exit;
	}		
	
	function invalid_item(){
		if(!User::is_login()){
			echo "not_login";
			exit;
		}
		else{
			$item_id= (int)Url::get('id');
			$item_type= (int)Url::get('type',2);			
			if($item_id){
				$item 			= Item::get_item($item_id);
				$item_memcache 	= $item;
			}	
			else{
				$item=array();
			}	
			if($item && (User::have_permit(ADMIN_ITEM) || User::have_cat_permit($item['category_id']))){
				$type = 2;
			}elseif(!User::have_permit(ADMIN_ITEM) && User::is_mod()){
				$type = 3;
			}
			
			if (!User::have_permit(ADMIN_ITEM) && User::is_mod() && DB::count('bad_content','id_item ='.$item_id.' AND type = 3 AND user_id = '.User::id())){
				echo "dup_invalid";	
				exit;
			}
			
			$reason = AZLib::getParam('reason');
			if (User::is_mod() && !$reason){
				echo "empty";	
				exit;
			}
			$reason 			= $reason?$reason:"Tin đang bị kiểm duyệt";
			$arr_data = array(
								'type' =>  $type,
								'id_item' 	=> $item_id,
								'user_id' 	=> User::id(),
								'user_name' => User::user_name(),
								'reason' 	=> $reason,
								'time_post' => TIME_NOW
								);
			if (!User::have_permit(ADMIN_ITEM) && User::is_mod() && DB::insert('bad_content',$arr_data)){
				echo "success_invalid";	
				exit;
			}
					
			if($item && (User::have_permit(ADMIN_ITEM) || User::have_cat_permit($item['category_id'])) && DB::insert('bad_content',$arr_data)){
				AZLib::getCats();
				
				$link_help = 'http://help.enbac.com/index.php/Trang_%C4%91%E1%BA%A7u';
				/*if (CGlobal::$allCategories[$item['category_id']]['link_help']){
					$link_help = CGlobal::$allCategories[$item['category_id']]['link_help'];
				}
				elseif (isset(CGlobal::$allCategories[$item['category_id']]['parent_id']) && CGlobal::$allCategories[CGlobal::$allCategories[$item['category_id']]['parent_id']]['link_help'])
				{
					$link_help = CGlobal::$allCategories[CGlobal::$allCategories[$item['category_id']]['parent_id']]['link_help'];
				}*/
				
				switch($item['level_1_catid'])
				{
					case "30": 
					case "1": 
					case "31": 
					case "107": 
					case "78": 
					case "51":
						$link_help = 'http://help.enbac.com/index.php/Quy_%C4%91%E1%BB%8Bnh_ki%E1%BB%83m_duy%E1%BB%87t_tin_m%E1%BB%A5c_Th%E1%BB%9Di_trang_Nam/_N%E1%BB%AF%2C_Teen%2C_M%E1%BA%B9_v%C3%A0_b%C3%A9%2C_Gi%C3%A0y_d%C3%A9p_n%E1%BB%AF%2C_M%E1%BB%B9_ph%E1%BA%A9m_trang_s%E1%BB%A9c';
						break;
					case "451": 
					case "105": 
					case "592": 
					case "447": 
					case "100":
						$link_help = 'http://help.enbac.com/index.php/Quy_%C4%91%E1%BB%8Bnh_ki%E1%BB%83m_duy%E1%BB%87t_tin_m%E1%BB%A5c_N%E1%BB%99i_th%E1%BA%A5t_gia_%C4%91%C3%ACnh/_V%C4%83n_ph%C3%B2ng%2C_%C4%90%E1%BB%93_d%C3%B9ng_b%E1%BA%BFp%2C_V%E1%BA%ADt_li%E1%BB%87u_x%C3%A2y_d%E1%BB%B1ng%2C_Ph%C3%B2ng_t%E1%BA%AFm_v%C3%A0_v%E1%BB%87_sinh';
						break;
					case "83": 
					case "541":
						$link_help = 'http://help.enbac.com/index.php/Quy_%C4%91%E1%BB%8Bnh_ki%E1%BB%83m_duy%E1%BB%87t_tin_m%E1%BB%A5c_%C4%90i%E1%BB%87n_tho%E1%BA%A1i%2C_Ch%E1%BB%A3_sim';
						break;
					case "104":
						$link_help = 'http://help.enbac.com/index.php/Quy_%C4%91%E1%BB%8Bnh_ki%E1%BB%83m_duy%E1%BB%87t_tin_m%E1%BB%A5c_H%C3%A0ng_hi%E1%BB%87u';
						break;
					case "331": 
					case "332": 
					case "335": 
					case "337":
						$link_help = 'http://help.enbac.com/index.php/Quy_%C4%91%E1%BB%8Bnh_ki%E1%BB%83m_duy%E1%BB%87t_tin_m%E1%BB%A5c_%C3%94_t%C3%B4';
						break;
					case "79":
						$link_help = 'http://help.enbac.com/index.php/Quy_%C4%91%E1%BB%8Bnh_ki%E1%BB%83m_duy%E1%BB%87t_tin_m%E1%BB%A5c_%C4%90i%E1%BB%87n_m%C3%A1y';
						break;
					case "103": 
					case "579": 
					case "571":
						$link_help = 'http://help.enbac.com/index.php/Quy_%C4%91%E1%BB%8Bnh_ki%E1%BB%83m_duy%E1%BB%87t_tin_m%E1%BB%A5c_Nh%C3%A0_%C4%91%E1%BA%A5t';
						break;
					case "514": 
					case "521":
						$link_help = 'http://help.enbac.com/index.php/Quy_%C4%91%E1%BB%8Bnh_ki%E1%BB%83m_duy%E1%BB%87t_tin_m%E1%BB%A5c_D%E1%BB%8Bch_v%E1%BB%A5_%28DV_th%E1%BB%9Di_trang/l%C3%A0m_%C4%91%E1%BA%B9p%2C_DV_%C4%90i%E1%BB%87n_m%C3%A1y%29';
						break;
					case "109": 
					case "101":
						$link_help = 'http://help.enbac.com/index.php/Quy_%C4%91%E1%BB%8Bnh_ki%E1%BB%83m_duy%E1%BB%87t_tin_m%E1%BB%A5c_T%E1%BB%95ng_h%E1%BB%A3p%2C_%C4%90i_ch%E1%BB%A3_online';
						break;
				}
				
				DB::query("DELETE FROM bad_content WHERE type = 3 AND id_item = ".$item_id);
					
				DB::query("UPDATE item SET status = ".$item_type." , valid_time=".TIME_NOW.', valid_user="'.User::user_name().'" WHERE id = '.$item_id);
				
				if(MEMCACHE_ON){
					$item_memcache['status'] 			= $item_type;
					$item_memcache['valid_time'] 		= TIME_NOW;
					$item_memcache['valid_user'] 		= User::user_name();
					AZMemcache::do_put("item:$item_id",$item_memcache);
				}
				
				$receiver_user_id 	= (int)Url::get('receiver_user_id');				
				$receiver_user 		= DB::select('user','id = '.$receiver_user_id);
				
				$title_pm = 'Thông báo từ EnBac';
				
				$content_pm = 'Tin [url='.WEB_ROOT.AZRewrite::formatUrl('?page=item_detail&id='.$item_id.'&ebname='.AZLib::safe_title($item['name'])).']'.$item['name'].'[/url] của bạn đã bị kiểm duyệt với lý do: '.$reason.'
				Bạn click [url='.$link_help.'][b]vào đây[/b][/url] để xem quy định đăng tin! Hoặc  [url=http://blog.enbac.com/?p=28][b] vào đây [/b][/url] để xem hướng dẫn đăng tin
				Đề nghị bạn hãy sửa lại tin này cho hợp lệ.
				
				Xin cảm ơn bạn.
				
				Vui lòng không trả lời tin nhắn này!';
				
				if(User::$current->data['signature']) $content_pm .= '<br /><hr /><br />Mọi thắc mắc vui lòng liên hệ theo số ĐT: 04.39743410 - Số máy lẻ: ' . User::$current->data['signature'];
				
				$id_mess = AZLib::Send_pm(User::id(), User::user_name(), $receiver_user, $title_pm, $content_pm,true);
				
				$from =  AZLib::getParam('from');
				
				if($from=='admin'){
					echo AZLib::getParam('receiver_user_id').'_'.$item_id;
				}
				else {
					echo $item_id;	
				}
			}
			else{
				echo "fail";
			}
		}			
		exit;
	}

	
	function fn_reset_pas(){
		$user_id = AZLib::getParam('user_id');
		$user 	 = User::getUser($user_id);
		
		if(!$user_id || !$user || !User::have_permit(ADMIN_USER)){
			echo "no_perm";
			exit;
		}								
		
		$gid 		  = User::check_admin($user['gids']);
		
		if($user_id !=User::id() && ($gid == 9 || ($gid==1 && !User::is_root()) )){//root ko đc đổi pass của root, admin ko đc đổi pass admin
			echo "no_perm";
			exit;
		}
		
		$checked 	= AZLib::getParam('checked');		
		//AZLib::getParam('pas');//Chú ý đối với mật khẩu ko đc dùng qua hàm này vì dữ liệu sẽ bị biến dạng
		$pas 		= Url::get('pas');
		$user_name 	= $user['user_name'];		
		
		if($checked=='on'){
			$messenger=file_get_contents('templates/ForgotPassword/reset_password.html');
			$message=str_replace('[[|user_name|]]',$user_name,$messenger);
			$message=str_replace('[[|password|]]',$pas,$message);
			$subject = 'Khôi phục mật khẩu!';
			
			$row = DB::fetch('SELECT email FROM user WHERE id="'.$user_id.'"');	
			
			if(System::sendEBEmail($row['email'],$subject,$message)){
					$id_update = DB::update('user',array('password'=>User::encode_password($pas)),"id =$user_id");
					if($id_update){
						echo "success";
						exit;
					}
				}
			else
				{
					echo "unsuccess";
					exit;
				}		
		}
		else{
			$id_update = DB::update('user',array('password'=>User::encode_password($pas)),'id ="'.$user_id.'"');
			if($id_update){
				echo "success";
				exit;
			}
		}				
	}	
	
	function fn_exit_no_source_item(){
		if(!User::is_login()){
			echo "not_login";
			exit;
		}		
		
		$item_id= (int)Url::get('item_id');
		
		if($item_id){
			$item 			= Item::get_item($item_id);
			$item_memcache 	= $item;
		}	
		else 
			$item=array();
		
		if($item && (User::have_permit(ADMIN_ITEM) || User::have_cat_permit($item['category_id']))){	
			if(DB::update('item',array('brief'=>""),'id='.$item_id)){
				if(MEMCACHE_ON){
					$item_memcache['brief'] 			= "";
					AZMemcache::do_put("item:$item_id",$item_memcache);
				}
				
				echo "success";
				exit;
			}
			else{
				echo "unsuccess";
				exit;
			}
		}
		else {
			echo "no_perm";
			exit;
		}
	}
	
	function fn_unban_nick_profile(){
		$user_id = (int)Url::get('user_id',0);
		$user 	 = User::getUser($user_id);
		
		if(!$user_id || !$user || !User::have_permit(array(ADMIN_USER))){
			echo "no_perm";
			exit;
		}	
		
		if($user_id){							
			DB::update('user',array('block_time'=>TIME_NOW),"id = $user_id");
			
			$user_lock = DB::fetch('SELECT id FROM user_lock WHERE user_id='.$user_id." ORDER BY id DESC LIMIT 1");
			DB::update('user_lock',array('unlock_time'=>TIME_NOW,'unlock_user'=>User::user_name()),'id = '.$user_lock["id"]);
			
			// tha Kiem duyet tin khi khoa thanh vien
			$arr_data = array('user_id'=>$user_id);
			$this->lockItemWhenLockAccount($arr_data, 0);
			// end tha kiem duyet tin khi khoa thanh vien
			
			User::getUser($user_id,0,1);
			echo "success";
			exit;
		}
	}
	
  	function fn_active_user(){
		if(!User::is_login()){
			echo "not_login";
			exit;
		}
		
		if(!User::have_permit(ADMIN_USER)){
			echo "no_perm";
			exit;
		}	
		
		$user_id = AZLib::getParam('user_id');
		$gids 	 = AZLib::getParam('gids');
		$action 	 = AZLib::getParam('action');	
			
		if(!User::check_higher_permis(User::$current->data['gids'],$gids)){
			echo "no_perm";
			exit;
		}
		
		if($user_id && $action=='de_active'){
			$user_info = DB::fetch('SELECT password FROM user WHERE id="'.$user_id.'"');
			$active_code = md5(TIME_NOW.$user_info['password']);
			
			$id_update = DB::update('user',array('is_active'=>1),'id="'.$user_id.'"');
			$id_insert = DB::insert('user_active',array('user_id'=>$user_id,'active_code'=>$active_code,'time'=>TIME_NOW));
		
			if($id_update && $id_insert ){
				echo "success";
				exit;
			}else{
				echo "unsuccess";
				exit;
			}		
		}
		elseif($user_id && $action=='active'){
			$id_update = DB::update('user',array('is_active'=>0),'id="'.$user_id.'"');
			//$id_delete = DB::delete('user_active','user_id="'.$user_id.'"');
			
			if($id_update ){
				echo "success";
				exit;
			}else{
				echo "unsuccess";
				exit;
			}
			
		}
		else{
			echo "unsuccess";
			exit;
		}
  	}

  	function fn_send_email_active(){
  
	if(!User::is_login()){
			echo "not_login";
			exit;
	}
	
	if(!User::have_permit(ADMIN_USER)){
		echo "no_perm";
		exit;
	}	
	
	$user_id = AZLib::getParam('user_id');
	
	if($user_id){
		$user_info = DB::fetch('SELECT a.is_active, a.user_name,a.email,aa.active_code FROM user a, user_active aa WHERE a.id="'.$user_id.'" AND aa.user_id="'.$user_id.'"');
		if($user_info['is_active']==1){									
			global $display;
			$display->add('eb_url',WEB_ROOT);
			$display->add('user_id',$user_id);
			$display->add('user_name',$user_info['user_name']);
			$display->add('active_code',$user_info['active_code']);			
			$content_email = $display->output('send_active_mail',1,'RegisterSuccess');
			
			if(System::sendEBEmail($user_info['email'],'Kích hoạt tài khoản',$content_email,'Enbac.com')){
				echo "success";
				exit;
			}
			else{
				echo "unsuccess";
				exit;
			}
		}		
	}
	else{
		echo "unsuccess";
		exit;
	}	  	
  }
    
  	function fn_add_up_item($status=0){//0: trừ lượt up, 1: cộng lượt up
		$user_id 	= (int)Url::get('user_id');
		$up_count 	= (int)Url::get('up_count');
		$up_price 	= (int)Url::get('up_price');
		$up_type 	= (int)Url::get('up_type');
		$up_note 	= Url::get('up_note');
			
		if(!User::is_login()){
			echo "not_login";
			exit;
		}
		
		if(!User::have_permit(ADMIN_USER)){
			echo "no_perm";
			exit;
		}	
		
		if($up_count>0){
			$user 	= DB::fetch('SELECT id, user_name, up_item FROM user WHERE id = '.$user_id);
			$admin 	= User::$current->data;
		
			
			if($status==0){//Trừ lượt up
				if($user['up_item']<$up_count){
					$up_count  = 0-$user['up_item'];
				}
				else{
					$up_count  = 0-$up_count;
				}
				
				if($up_price>0)
				{
					$up_price = 0-$up_price;
				}
				
				$update = DB::query('UPDATE user SET up_item=up_item - '.abs($up_count).' WHERE id='.$user_id);
			}
			else{//Cộng lượt up
				$update = DB::query('UPDATE user SET up_item=up_item+'.$up_count.' WHERE id='.$user_id);
			}
			$up_counts = $user['up_item'] + $up_count;
			//$update = DB::query('UPDATE user SET up_item='.$up_counts.' WHERE id='.$user_id);
			
			if($update){
				User::getUser($user_id,0,1);
				
				$up_log_sql = "INSERT INTO up_log (`user_id`,	`user_name`,			`admin_id`,		`admin_name`,			`time`,		`up_count`,`up_before`,			`up_after`,	`price`,	`type`,		`note`) 
										VALUES ({$user['id']},	'{$user['user_name']}',{$admin['id']},	'{$admin['user_name']}',".TIME_NOW.",$up_count,'{$user['up_item']}',$up_counts,	$up_price,	$up_type,	'$up_note')";
			
				DB::query($up_log_sql);
				echo $up_counts;
				exit;
			}
			else{
				echo 'unsuccess';
				exit;
			}
		}
		else{
			echo "invalid";
		exit;
		}
	}
	
	function edit_phone_verify(){
		$sms_id 	= (int)Url::get('sms_id',0);		
		$sms_modify = AZLib::trimSpace(Url::get('sms_modify',''));		
		
		if(!User::is_login()){
			echo "not_login";
			exit;
		}	
		if(!User::is_admin()){
			echo "no_perm";
			exit;
		}	
		
		if($sms_id>0){
			$sms = DB::fetch('SELECT id, phone_number, phone_number_modify, verify FROM phone_active WHERE id = '.$sms_id);
			if($sms && $sms['verify']==0){
				
				$sms['phone_number_modify'] = $sms_modify;
				$sms['modify_by'] 			= User::user_name();
				$sms['modify_time'] 		= TIME_NOW;
				
				if(DB::update('phone_active',$sms,'id='.$sms_id)){
					exit;
				}
			}
			else{
				echo 'unsuccess';
				exit;
			}
		}
		else{
			echo "invalid";
			exit;
		}
	}
	
	function note_phone_verify(){
		$sms_id   = (int)Url::get('sms_id',0);		
		$sms_note = AZLib::trimSpace(Url::get('sms_note',''));		
		
		if(!User::is_login()){
			echo "not_login";
			exit;
		}	
		if(!User::have_permit(PHONE_VERIFY)){
			echo "no_perm";
			exit;
		}	
		
		if($sms_id>0){
			$sms = DB::fetch('SELECT id, modify_by, modify_time, note FROM phone_active WHERE id = '.$sms_id);
			if($sms){
				$sms['modify_by'] 			= User::user_name();
				$sms['modify_time'] 		= TIME_NOW;
				$sms['note'] 				= addslashes($sms_note);
				
				if(DB::update('phone_active',$sms,'id='.$sms_id)){
					echo 'success';
					exit;
				}
			}
			else{
				echo 'unsuccess';
				exit;
			}
		}
		else{
			echo "invalid";
			exit;
		}
	}
	
	function note_sms_phone_verify(){
		$sms_id   = (int)Url::get('sms_id',0);		
		$sms_note = AZLib::trimSpace(Url::get('sms_note',''));		
		
		if(!User::is_login()){
			echo "not_login";
			exit;
		}	
		if(!User::have_permit(PHONE_VERIFY)){
			echo "no_perm";
			exit;
		}	
		
		if($sms_id>0){
			$sms = DB::fetch('SELECT id, m_user_name, m_time, note FROM sms_phone_verify WHERE id = '.$sms_id);
			if($sms){
				$sms['m_user_name'] = User::user_name();
				$sms['m_time'] 		= TIME_NOW;
				$sms['note'] 		= addslashes($sms_note);
				
				if(DB::update('sms_phone_verify',$sms,'id='.$sms_id)){
					echo 'success';
					exit;
				}
			}
			else{
				echo 'unsuccess';
				exit;
			}
		}
		else{
			echo "invalid";
			exit;
		}
	}
	function note_user_active_sms(){
		$sms_id   = (int)Url::get('sms_id',0);		
		$sms_note = AZLib::trimSpace(Url::get('sms_note',''));		
		
		if(!User::is_login()){
			echo "not_login";
			exit;
		}	
		if(!User::have_permit(PHONE_VERIFY)){
			echo "no_perm";
			exit;
		}	
		
		if($sms_id>0){
			$sms = DB::fetch('SELECT id, m_time,m_user_name, note FROM sms_user_active_detail WHERE id = '.$sms_id);
			if($sms){
				$sms['m_user_name'] = User::user_name();
				$sms['m_time'] 		= TIME_NOW;
				$sms['note'] 		= addslashes($sms_note);
				
				if(DB::update('sms_user_active_detail',$sms,'id='.$sms_id)){
					echo 'success';
					exit;
				}
			}
			else{
				echo 'unsuccess';
				exit;
			}
		}
		else{
			echo "invalid";
			exit;
		}
	}

        function note_payment_ebp(){
		$id   = (int)Url::get('id',0);
		$note = AZLib::trimSpace(Url::get('note',''));

		if(!User::is_login()){
			echo "not_login";
			exit;
		}
		if(!User::have_permit(SMS_UPTIN)){
			echo "no_perm";
			exit;
		}

		if($id>0){
			$row = DB::fetch('SELECT id, note FROM payment_ebp WHERE id = '.$id);
			if($row){
				$row['user_modify'] = User::user_name();
				$row['time_modify'] 		= TIME_NOW;
				$row['note'] 		= $note?addslashes($note):'';

				if(DB::update('payment_ebp',$row,'id='.$id)){
					echo 'success';
					exit;
				}
			}
			else{
				echo 'unsuccess';
				exit;
			}
		}
		else{
			echo "invalid";
			exit;
		}
	}
	
	function note_sms_uptin(){
		$sms_id   = (int)Url::get('sms_id',0);		
		$sms_note = AZLib::trimSpace(Url::get('sms_note',''));		
		
		if(!User::is_login()){
			echo "not_login";
			exit;
		}	
		if(!User::have_permit(SMS_UPTIN)){
			echo "no_perm";
			exit;
		}	
		
		if($sms_id>0){
			$sms = DB::fetch('SELECT id, note FROM sms_uptin WHERE id = '.$sms_id);
			if($sms){
				$sms['m_user_name'] = User::user_name();
				$sms['m_time'] 		= TIME_NOW;
				$sms['note'] 		= $sms_note?"<b>".User::user_name().":</b> ".addslashes($sms_note):'';
				
				if(DB::update('sms_uptin',$sms,'id='.$sms_id)){
					echo 'success';
					exit;
				}
			}
			else{
				echo 'unsuccess';
				exit;
			}
		}
		else{
			echo "invalid";
			exit;
		}
	}
	
	function fn_processed_bad_content(){
		if(!User::have_permit(ADMIN_USER)){
			echo 'no_perm';	
			exit;
		}				
		
		$id   = (int)Url::get('id',0);				
		$status  = (int)Url::get('status',0);
		$note  = AZLib::trimSpace(Url::get('note',''));				
		
		if($id>0 && ($status==0 || $status==1)){			
			
			if($status==1){
			
				$item 	= DB::select('bad_content','id = '.$id);
				
				$receiver_user 	= DB::select('user','id = '.$item["user_id"]);
				$title_pm = 'Thông báo từ EnBac';
				$content_pm = 'Cảm ơn bạn đã than phiền một tin với nội dung "'.$item["reason"].'". 
				
				Chúng tôi đã xử lý:  '.$note.'
				
				Rất mong sự đóng góp của bạn để én bạc ngày càng phát triển hơn.
				
				BQT én bạc!
				
				Vui lòng không trả lời tin nhắn này!';	
				
				AZLib::Send_pm(User::id(), User::user_name(), $receiver_user, $title_pm, $content_pm,true);	
			
			}
						
			if(DB::query("UPDATE bad_content SET status=$status, admin_id=".User::id().", admin_name='".User::user_name()."', note='".$note."' WHERE id=$id")){
				echo "success";
				exit;
			}
			else{
				echo "unsuccess";
				exit;
			}
		}
		
	}
	
	function fn_tracking_user(){
		if(!User::have_permit(ADMIN_USER)){
			echo "no_perm";
			exit;
		}
		$id   = (int)Url::get('id',0);
		$type   = (int)Url::get('type',0);
		
		if($id>0){
			$id_update = DB::query("UPDATE user SET tracking=$type WHERE id=$id");
			if($id_update){
				User::getUser($id,0,1);
				echo "success";
				exit;	
			}
			else{
				echo "unsuccess";
				exit;	
			}			
		}
		else{
				echo "unsuccess";
				exit;	
		}
				
	}	
	
	function fn_tag_search(){
		if(!User::have_permit(ADMIN_TAG_SEARCH)){
			echo "no_perm";
			exit;
		}				
		require_once 'modules/ItemDetail/forms/ItemDetail.php';	
		AZLib::getCats();
		
		$id = (int)Url::get('id',0);
		$hit = (int)Url::get('hit',0);		
		$contents = Url::get('contents','');
		$type = Url::get('type','');
		$cat_id = (int)Url::get('cat_id',0);
		$cat_parent_id = CGlobal::$allCategories[$cat_id]["parent_id"];
		
		if($type=="add"){
			if($contents){
				$new_row = array('keywords'=>$contents,'cat_id'=>$cat_id);	
				$id_insert = DB::insert('tag_search',$new_row );								
				if($id_insert){					
					ItemDetailForm::get_tag_search($cat_id, $cat_parent_id ,1,0);					
					echo "success";
					exit;
				}
				else{
					echo "unsuccess";
					exit;
				}
			}			
		}
		elseif($type=="edit"){
			if($contents && $id>0){
				$edit_row = array('keywords'=>$contents,'cat_id'=>$cat_id,'hit'=>$hit);	
				$id_update = DB::update('tag_search',$edit_row,'id='.$id);				
				if($id_update){					
					ItemDetailForm::get_tag_search($cat_id , $cat_parent_id, 1, 0);
					echo "success";
					exit;
				}
				else{
					echo "unsuccess";
					exit;
				}
			}						
		}				
	}
	
	function fn_del_tag_search(){		
		if(!User::have_permit(ADMIN_TAG_SEARCH)){
			echo "no_perm";
			exit;
		}
		
		AZLib::getCats();
		
		$id = (int)Url::get('id',0);
		$cat_id = (int)Url::get('cat_id',0);
		$cat_parent_id = CGlobal::$allCategories[$cat_id]["parent_id"];	
			
		if($id>0){
			require_once 'modules/ItemDetail/forms/ItemDetail.php';
			$id_del = DB::query("DELETE FROM tag_search WHERE id=".$id);
			if($id_del){				
				ItemDetailForm::get_tag_search($cat_id , $cat_parent_id, 1, 0);
				echo $id;
				exit;
			}
		}
	}
	function fn_lock_topic(){	
		if(!User::is_login()){
			echo "no_perm";
			exit;
		}
		if(!User::have_permit(ADMIN_ITEM) && !User::is_mod()){
			echo "no_perm";
			exit;
		}
	
		$id = (int)Url::get('id',0);
		$type = AZLib::getParam('type');
		$reason_lock = AZLib::getParam('reason_lock');
		$value = ($type=='lock')?1:0;		
		if($id>0){	
			$item 			= Item::get_item($id);
			$item_memcache 	= $item;
			
			if($item){
				if (!User::have_permit(ADMIN_ITEM) && !User::have_cat_permit($item['category_id']))
				{
					echo "no_perm";
					exit;
				}
				DB::query("UPDATE item SET state=$value, valid_time = ".TIME_NOW.", valid_user = '".User::user_name()."', up_time = ".($item["up_time"]-TIME_DOWN)." WHERE id=".$id);								
				if(MEMCACHE_ON){
					$item_memcache['state'] 			= $value;
					$item_memcache['valid_time'] 		= TIME_NOW;
					$item_memcache['valid_user'] 		= User::user_name();
					
					
					AZMemcache::do_put("item:$id",$item_memcache);
				}
				
				if($type=='lock'){
					AZLib::getCats();
					
					$link_help = 'http://help.enbac.com/index.php/Quy_%C4%91%E1%BB%8Bnh_%C4%91%C4%83ng_tin';
					if (CGlobal::$allCategories[$item['category_id']]['link_help']){
						$link_help = CGlobal::$allCategories[$item['category_id']]['link_help'];
					}
					elseif (isset(CGlobal::$allCategories[$item['category_id']]['parent_id']) && CGlobal::$allCategories[CGlobal::$allCategories[$item['category_id']]['parent_id']]['link_help']){
						$link_help = CGlobal::$allCategories[CGlobal::$allCategories[$item['category_id']]['parent_id']]['link_help'];
					}
					$receiver_user 	= DB::select('user','id = '.$item["user_id"]);
					
					$title_pm = 'Thông báo từ EnBac';
						
					$content_pm = 'Tin [url='.WEB_ROOT.AZRewrite::formatUrl('?page=item_detail&id='.$id.'&ebname='.AZLib::safe_title($item['name'])).']'.$item['name'].'[/url] của bạn đã bị khóa với lý do: '.$reason_lock.'
					
					Bạn click [url='.$link_help.'][b]vào đây[/b][/url] để xem quy định đăng tin! Hoặc [url=http://blog.enbac.com/?p=28] [b]vào đây[/b][/url] để xem hướng dẫn đăng tin. 
					
					Ban quản trị Én bạc.
					
					Vui lòng không trả lời tin nhắn này!';													
					
					AZLib::Send_pm(User::id(), User::user_name(), $receiver_user, $title_pm, $content_pm,true);	
				
					$item_array = array(
										'item_id' => $id,								
										'user_id' => User::id(),
										'user_name' => User::user_name(),
										'lock_time' => TIME_NOW,
										'reason_lock' => $reason_lock
										);
					DB::insert('item_lock',	$item_array);					
				}
				elseif($type=='unlock'){
					DB::delete('item_lock','item_id='.$id);
				}
				
				echo $type;
				exit;
			}
			else{
				echo "unsuccess";
				exit;
			}
		}
		else{
			echo "unsuccess";
			exit;
		}
					
	}	
	function fn_del_reason(){	
		if(!User::is_login()){
			echo "not_login";
			exit;
		}
		if(!User::have_permit(ADMIN_ITEM)){
			echo "no_perm";
			exit;
		}
	
		$id = (int)Url::get('id',0);
		if (AZLib::del_reason_mod($id)){
			echo $id;
			exit();
		}
		else{
			echo "unsuccess";
			exit;
		}
					
	}
		
	function fn_del_item_reason(){	
		if(!User::is_login()){
			echo "not_login";
			exit;
		}
		if(!User::have_permit(ADMIN_ITEM) && !User::is_mod()){
			echo "no_perm";
			exit;
		}
	
		$id = (int)Url::get('id',0);
				
		if($id){
			$item 			= Item::get_item($id);
			$item_memcache 	= $item;
		}
		else 
			$item=array();
		
		if($item && (User::have_permit(ADMIN_ITEM) || User::have_cat_permit($item['category_id']))){
			Item::delete_item($id);
			AZLib::del_reason_mod($id);			
			echo $id;
			exit;			
				
		}else {
			echo "no_perm";
			exit;
		}	
		echo "unsuccess";
		exit;
					
	}
	
	function fn_valid_all_item(){
	
		if(!User::is_login()){
			echo "not_login";
			exit;
		}
		if(!User::have_permit(ADMIN_ITEM) && !User::is_mod()){
			echo "no_perm";
			exit;
		}
		
		$user_id = (int)Url::get('user_id',0);
		$type = (int)Url::get('type',2);
		$reason = Url::get('reason',"");
		$time_valid = TIME_NOW;
		$admin_id = User::id();
		$admin_name = User::user_name();
		
		if($reason!="" && $user_id>0 && $type){
		
			if(MEMCACHE_ON){		
				$sql 	= "SELECT * FROM item WHERE user_id = $user_id AND status = 1";
				$re 	= DB::query($sql);				
				while($item_memcache = mysql_fetch_assoc($re)){
					$item_memcache['status'] 			= $type;
					$item_memcache['valid_time'] 		= $time_valid;
					$item_memcache['valid_user'] 		= $admin_name;
					AZMemcache::do_put("item:".$item_memcache['id'],$item_memcache);
				}
			}
			
			// UPDATE table bad_content
			$str_sql = "";			
			$sql = "SELECT id FROM item WHERE user_id = $user_id AND status = 1";
			$re 	= DB::query($sql);
			while($item = mysql_fetch_assoc($re)){
				$str_sql .= ($str_sql?",":"")."($type, {$item['id']}, $admin_id, '$admin_name', '$reason', $time_valid)";
			}
			
			$str_insert = "INSERT INTO bad_content(type, id_item, user_id, user_name, reason, time_post) VALUES ".$str_sql;
			DB::query($str_insert);
			// end UPDATE table bad_content
														
			$sql = "UPDATE item SET status = $type, valid_time = $time_valid, valid_user = '$admin_name' WHERE user_id=$user_id AND status=1";
			$re = DB::query($sql);			
			if($re){
				//$receiver_user = DB::select('user','id = '.$user_id);
				$receiver_user 		=  User::getUser($user_id);
				$title_pm = 'Thông báo từ EnBac';
				$content_pm = 'Tất cả các tin của bạn đã bị kiểm duyệt với lý do: '.$reason.'
				
				[hr]Bạn click [url=http://blog.enbac.com/2008/10/14/quy-che-dang-tin-tren-enbaccom.html][b]vào đây[/b][/url] để xem quy định đăng tin! Hoặc [url=http://blog.enbac.com/?p=28][b]vào đây[/b][/url] để xem hướng dẫn đăng tin
				
				[hr]Đề nghị bạn hãy thực hiện đúng yêu cầu của chúng tôi.
				
				[hr]Xin cảm ơn bạn.
				
				[hr]Vui lòng không trả lời tin nhắn này!';												
				$id_mess = AZLib::Send_pm(User::id(), User::user_name(), $receiver_user, $title_pm, $content_pm,true);
				
				echo "success";		
				exit();
			}			
			
		}
		else{
			echo "unsuccess";
			exit();
		}
		
	}
	function fn_add_product_hot(){
		if(!User::is_login()){
			echo "not_login";
			exit;
		}
		if(!User::have_permit(ADMIN_ITEM)){
			echo "no_perm";
			exit;
		}
		
		$item_id = (int)Url::get('item_id',0);
		$cat_id = (int)Url::get('cat_id',0);
		$start_date = Url::get('start_date');
		$end_date = Url::get('end_date');
		
		
		
		$end_date_arr = explode('-',$end_date);
		if(isset($end_date_arr[0]) && isset($end_date_arr[1]) && isset($end_date_arr[2])){
			$end_time = mktime(0,0,0,(int)$end_date_arr[1],(int)$end_date_arr[0],(int)$end_date_arr[2]);
		}
		else{
			$end_time =0;
		}
		$start_date_arr = explode('-',$start_date);
		if(isset($start_date_arr[0]) && isset($start_date_arr[1]) && isset($start_date_arr[2])){
			$start_time = mktime(0,0,0,(int)$start_date_arr[1],(int)$start_date_arr[0],(int)$start_date_arr[2]);
		}
		else{
			$start_time = ($end_time > 0) ? TIME_NOW : 0 ;
		}
		
		$status = ($end_time > 0) ? 1 : 0 ;
		
		$check_item	= DB::fetch('SELECT id,start_time,end_time FROM product_hot WHERE item_id ='.$item_id.' And product_hot_cat_id = "'.$cat_id.'" And status ='.$status);
		if ($check_item && $status == 0){
			echo "dup_item";
			exit;	
		}
		if($check_item && $status == 1){
			if ($start_time != TIME_NOW && $start_time != $check_item['start_time'] && $start_time != 0){
				$start_time_new = $start_time;
			}
			else{
				$start_time_new = $check_item['start_time'];
			}
			if ($end_time != $check_item['end_time'] && $end_time != 0){
				$end_time_new = $end_time;
			}
			else{
				$end_time_new = $check_item['end_time'];
			}
			
			$arrValues = array(
							   	'end_time'=>$end_time_new,
								'start_time'=>$start_time_new
							   );
			DB::update('product_hot',$arrValues,'id ='.$check_item['id']);
			echo "success";
			exit;
		}
		$arrValues = array(
						   'item_id'			=>$item_id,
						   'product_hot_cat_id'	=>$cat_id,
						   'status'				=>$status,
						   'end_time'			=>$end_time,
						   'start_time'			=>$start_time
						   );
		DB::insert('product_hot',$arrValues);
		echo "success";
		exit;
		
	}
	
	function fn_del_item_product_hot(){
		if(!User::is_login()){
			echo "not_login";
			exit;
		}
		if(!User::have_permit(ADMIN_ITEM)){
			echo "no_perm";
			exit;
		}
		$cat_id = (int)Url::get('cat_id',0);
		$item_id = (int)Url::get('item_id',0);
		$delete = DB::delete('product_hot','product_hot_cat_id = '.$cat_id.' AND item_id = '.$item_id.' AND status = 1');
		if ($delete){
			echo 'success';
		}
		else{
			echo 'unsuccess';
		}
		exit;
	}
	function fn_del_static_cache(){
		if(!User::is_login()){
			echo "not_login";
			exit;
		}
		if(!User::have_permit(ADMIN_ITEM)){
			echo "no_perm";
			exit;
		}
		$cache_file = Url::get('cache_file');
		StaticCache::delCache($cache_file);
		echo 'success';
		exit;
	}
	function fn_del_product_hot(){
		if(!User::is_login()){
			echo "not_login";
			exit;
		}
		if(!User::have_permit(ADMIN_ITEM)){
			echo "no_perm";
			exit;
		}
		$cat_id = (int)Url::get('cat_id',0);
		
		$delete = DB::delete('product_hot','product_hot_cat_id = '.$cat_id.' AND status = 0');
		if ($delete){
			echo 'success';
		}
		else{
			echo 'unsuccess';
		}
		exit;
	}
	
	function lockItemWhenLockAccount($value=array(),$is_lock=1){ // $is_lock = 1: lock, $is_lock = 0: Unlock
		
		$user_id 	= $value["user_id"];
		$reason_invalid = isset($value["reason_invalid"])?$value["reason_invalid"]:"";
		
		if($user_id){
			$user = User::getUser($user_id);
		}
		
		if($is_lock==0 && User::have_permit(array(ADMIN_USER))){
			$sql = 'SELECT id, admin_id, time FROM user_lock WHERE user_id = '.$user_id.' AND type = 0 ORDER BY id DESC LIMIT 1';
			$user_invalid = DB::fetch($sql);
			
			if(isset($user_invalid["id"])){
				$sql = "SELECT * FROM item WHERE user_id = $user_id AND state = 1";
				$re  = DB::query($sql);
				while($item = mysql_fetch_assoc($re)){
					$sql = "SELECT user_id, lock_time FROM item_lock WHERE item_id = {$item['id']} ORDER BY id DESC LIMIT 1";
					$re_lock = DB::query($sql);
					$item_lock = mysql_fetch_assoc($re_lock);
					
					if($item_lock["lock_time"]==$user_invalid["time"] && $item_lock["user_id"]==$user_invalid["admin_id"]){
						if(MEMCACHE_ON){
							$item_memcache = AZMemcache::do_get("item:{$item['id']}");
							$item_memcache['state'] = 0;
							AZMemcache::do_put("item:".$item_memcache['id'],$item_memcache);
						}
						DB::update('item',array('state'=>0),"id={$item['id']}");
					}
				}
			}
		}
		elseif($is_lock == 1){// chi thuc hien voi khoa theo ngay
			
			$sql_item = 'SELECT id FROM item WHERE user_id = '.$user_id.' AND status = 1 AND state=0';
			$re_item=DB::query($sql_item);
			
			while ($item = mysql_fetch_assoc($re_item)){
				$sql_insert = "INSERT INTO item_lock (item_id, user_id 	,user_name, lock_time, reason_lock) VALUES ({$item['id']},".User::id().",'".User::user_name()."',".TIME_NOW.",'".$reason_invalid."')";
				DB::query($sql_insert);	
			}
			
			if(MEMCACHE_ON){
				$sql 	= "SELECT * FROM item WHERE user_id = $user_id AND status = 1";
				$re 	= DB::query($sql);
				while($item_memcache = mysql_fetch_assoc($re)){
					$item_memcache['state'] 	= 1;
					AZMemcache::do_put("item:".$item_memcache['id'],$item_memcache);
				}
			}
			
			DB::update('item',array('state'=>"1"),'user_id = '.$user_id.' AND status = 1');
			
			$receiver_user 		=  User::getUser($user_id);
			$link_help = 'http://help.enbac.com/index.php/Quy_%C4%91%E1%BB%8Bnh_%C4%91%C4%83ng_tin';
			$title_pm = 'Thông báo từ EnBac';
			
			$content_pm = 'Tất cả tin đăng của bạn đã bị khóa với lý do: '.$reason_invalid.'
			
			Đề nghị bạn hãy thực hiện đúng các quy định của Én Bạc.
			
			Xin cảm ơn bạn.
			
			Vui lòng không trả lời tin nhắn này!';								
			
			AZLib::Send_pm(User::id(), User::user_name(), $receiver_user, $title_pm, $content_pm,true);			
		}
	}
	
	function user_same(){
		$json = '';
		if(!User::is_login()){			
			$json = '({"msg":"no_login"})';
			echo $json;
			exit;
		}
		if(!User::have_permit(ADMIN_ITEM)){
			$json = '({"msg":"no_perm"})';
			echo $json;
			exit();
		}		
		$user_id = (int)Url::get('user_id',0);
		$user_name = strtolower(trim(AZLib::getParam('user_name',"")));
		$note = AZLib::getParam('note',"");		
		$arr_user = DB::fetch("SELECT id FROM user WHERE user_name = '$user_name'");
		$user_id_add = $arr_user["id"];
		
		if(!$user_id_add){			
			$json = '({"msg":"not_exist"})';
			echo $json;
			exit();
		}
		
		if($user_id && $user_name){
			$arr_user_same = DB::fetch("SELECT id, group_same_id FROM user_same WHERE user_id=$user_id_add OR user_id = $user_id");
			if(!$arr_user_same["id"]){ // neu chua co thanh vien nao ca				
				$data_info = array(	
							'group_name'	=>$user_name,
							'date_created'	=>TIME_NOW,
							'admin_created'	=>User::user_name(),
							'user_ids'	=>$user_id_add.",".$user_id,
							'note'		=>$note
							);
				$id= DB::insert('user_same_group', $data_info);
				
				if($id){					
					$data_info = array(	
							'user_id'	=>$user_id_add,
							'group_same_id'	=>$id,
							'date_created'	=>TIME_NOW,
							'admin_created'	=>User::user_name(),
							'note'		=>$note		
							);
					DB::insert('user_same', $data_info);
					
					$data_info = array(	
							'user_id'	=>$user_id,
							'group_same_id'	=>$id,
							'date_created'	=>TIME_NOW,
							'admin_created'	=>User::user_name(),
							'note'		=>$note		
							);
					DB::insert('user_same', $data_info);
				}
								
				$json = '({"msg":"success"})';
				echo $json;
				exit();
						
			}
			else{
				$arr_user_same_other = DB::fetch("SELECT id, group_same_id FROM user_same WHERE user_id=$user_id_add");				
				if($arr_user_same_other["id"]){					
					$arr_group_other = DB::fetch("SELECT user_ids FROM user_same_group WHERE id = ".$arr_user_same_other["group_same_id"]);			
					$sql = "SELECT id, user_name FROM user WHERE id IN(".$arr_group_other["user_ids"].")";
					$str_value = "";
					$result = DB::query($sql);
					if($result){					
						while ($row=mysql_fetch_assoc($result)){
							$str_value .= ($str_value?", ":"").$row["user_name"];	
						}						
					}
					
					
					$json = '({"msg":"exist"';
					$json .= ',"list_user":"'.$str_value.'"';
					$json.="})";
					echo $json;
					exit();
				}
				else{
					$data_info = array(	
								'user_id'	=>$user_id_add,
								'group_same_id'	=>$arr_user_same["group_same_id"],
								'date_created'	=>TIME_NOW,
								'admin_created'	=>User::user_name(),
								'note'		=>$note		
								);
					DB::insert('user_same', $data_info);
					$arr_user_group = DB::fetch("SELECT note, user_ids FROM user_same_group WHERE id={$arr_user_same["group_same_id"]}");
					$user_ids = $arr_user_group["user_ids"].($arr_user_group["user_ids"]?",":"").$user_id_add;
					$note_plus = $arr_user_group["note"].($arr_user_group["note"]?", ":"").$note;
					$data_info = array(
								'group_name'	=>$user_name,
								'date_modify'	=>TIME_NOW,
								'admin_modify'	=>User::user_name(),
								'user_ids'	=>$user_ids,
								'note'		=>$note_plus
								);
					DB::update_id('user_same_group', $data_info, $arr_user_same["group_same_id"]);
					
					$json = '({"msg":"success"})';
					echo $json;
					exit();
				}
			}
		}
		
	}
	
	function del_user_same(){		
		if(!User::have_permit(ADMIN_ITEM)){
			echo "no_perm";
			exit;
		}
		$user_id = (int)Url::get('user_id',0);
		
		if($user_id){
			
			$str_value = "";	
			$arr_user = DB::fetch("SELECT group_same_id FROM user_same WHERE user_id = $user_id");
			DB::query("DELETE FROM user_same WHERE user_id = $user_id");
			$arr_group = DB::fetch("SELECT user_ids FROM user_same_group WHERE id = ".$arr_user["group_same_id"]);
			$arr_user_ids = explode(",",$arr_group["user_ids"]);
			foreach($arr_user_ids as $user_ids){
				if($user_ids!=$user_id){
					$str_value .= ($str_value?",":"").$user_ids;
				}
				if($str_value){
					$data_info = array(										
								'date_modify'	=>TIME_NOW,
								'admin_modify'	=>User::user_name(),
								'user_ids'	=>$str_value								
								);
																
					DB::update_id('user_same_group', $data_info, $arr_user["group_same_id"]);		
				}
			}
			
			echo $user_id;
			exit();
			
		}
		else{
			echo "unsuccess";
			exit();
		}
		
	}
	
	function admin_notice_user(){
		if(!User::have_permit(ADMIN_NOTICE_USER)){
			echo "no_perm";
			exit;
		}
		
		$user_id = (int)Url::get('user_id',0);
		$expire = (int)Url::get('expire',0);
		$expire = $expire?$expire:7;
		$content = AZLib::getParam('content',"");
				
		$user = User::getUser($user_id);
		
		if($user["id"]){
			$item_array = array(
						'content' => $content,
						'user_name' => $user["user_name"],
						'user_id' => $user_id,
						'admin_add' => User::user_name(),						
						'time_add' => TIME_NOW,
						'expire_date' => TIME_NOW + 86400*$expire,
				);
				
			DB::insert('admin_notice_user',	$item_array);
			User::getAdminNoticeUser($user_id,0,1);
		}
		else{
			echo "fail";
			exit();
		}								
	}
	
	function add_edit_ban_ip(){
		if(!User::have_permit(ADMIN_ITEM)){
			echo "no_perm";
			exit;
		}
		
		$user_name = User::user_name();
		$create_time = TIME_NOW;
		
		$ban_id = Url::get('ban_id',0);
		$ban_ip = Url::get('ban_ip',0);
		$ban_reason = AZLib::getParam('ban_reason',"");
				
		if($ban_ip){
			$ban_ip = str_replace(' ', '', $ban_ip );
			$item_array = array(
						'ip' => $ban_ip,
						'reason' => $ban_reason,
						'create_time' => $create_time,
						'user_name' => $user_name
				);
			if(!$ban_id){
				DB::insert('ban_ip',	$item_array);
				$ban_id = mysql_insert_id();
			}
			else {
				DB::update('ban_ip', $item_array,'id = '.$ban_id);
			}
			$item_array += array('id' => $ban_id);
			$item_array['create_time'] = date('d/m - h:i',$item_array['create_time']);
			
			// cho mang vao memcache
			$sql = "SELECT * FROM ban_ip Order by id desc";
			$result = DB::query($sql);
			while ($row = mysql_fetch_assoc($result)) {
				$row['create_time'] = date('d/m - h:i',$row['create_time']);
				$arr_mem_cache[$row['ip']] = $row;
			}
			AZMemcache::do_put('list_ban_ip',$arr_mem_cache);
			
			echo json_encode($item_array);
		}
		else{
			echo "not_ip";
		}
		exit();							
	}
	
	function xyly_tinspam(){
		if(!User::have_permit(ADMIN_ITEM)){
			echo "no_perm";
			exit;
		}
		$spam_id = Url::get('item_id',0);
		$user_name = User::user_name();
		$user_id = User::id();
		$create_time = TIME_NOW;
		if($spam_id){
			$item_array = array(
						'admin_id' => $user_id,
						'admin_name' => $user_name,
						'admin_time' => TIME_NOW
				);
			DB::update('item_spam', $item_array,'id = '.$spam_id);
			
		}
		$item_array['admin_time'] = date('d/m - h:i',$item_array['admin_time']);
		echo "done";
		exit();							
	}

    function update_sale_name(){
        if(!User::is_login()){
			echo "not_login";
			exit();
		}
        
        if(!User::have_permit(ADMIN_SUPER_SERVICE)){
			echo "no_permission";
			exit;
		}
        
        $idOrder = Url::get('id_order',0);
        $sale_name = trim(Url::get('sale_name',""));
        if($idOrder && $sale_name != ""){
            DB::update('order_services', array('sale_name' => $sale_name),'id = '.$idOrder);
        }
        exit();
    }
    
    function update_exist_acc(){
        if(!User::is_login()){
			echo "not_login";
			exit();
		}

        if(!User::have_permit(ADMIN_SUPER_SERVICE)){
			echo "no_permission";
			exit;
		}

        $idOrder = Url::get('id_order',0);
        $exist_acc = trim(Url::get('exist_acc',""));

//        if($exist_acc != ""){
//            $checkAcc = DB::select('user', "user_name='{$exist_acc}'");
//            if(!$checkAcc){
//                echo "no_exist_user";
//                exit;
//            }
//        }

        DB::update('order_services', array('exist_user' => $exist_acc),'id = '.$idOrder);
        exit();
    }

    
    function update_cpc_left(){
        if(!User::is_login()){
			echo "not_login";
			exit();
		}

        if(!User::have_permit(ADMIN_SUPER_SERVICE)){
			echo "no_permission";
			exit;
		}

        $idOrder    = Url::get('id_order',0);
        $cpc        = Url::get('cpc',0);
        $cpc_left   = Url::get('cpc_left');
        $type       = Url::get('type');

        if($type == 1){
            $cpc_left = $cpc_left + $cpc;
        }else{
            $cpc_left = $cpc_left - $cpc;
        }
        if($cpc_left <= 0){
            $cpc_left = -1;
        }
        DB::update('order_services', array('cpc_left' => $cpc_left),'id = '.$idOrder);
        exit();
    }

    function minus_take_care(){
        if(!User::is_login()){
			echo "not_login";
			exit();
		}

        if(!User::have_permit(ADMIN_SUPER_SERVICE)){
			echo "no_permission";
			exit;
		}

        $user_name = Url::get('user_name',"");
        $whereOrderCheck = "user_use='".$user_name."' AND (active = 1 OR active = 0)";
        $aryServiceInfo = DB::select('order_services', $whereOrderCheck);
        $take_care = $aryServiceInfo['take_care'];

        if($user_name && $take_care && $take_care != -1)
        {
            $take_care = $take_care - 1;
            DB::update('order_services', array('take_care' => $take_care),$whereOrderCheck);
        }else{
            echo "kgh";
        }
        exit();
    }
}//class
?>